//
//  RXPictureView.swift
//  weibo
//
//  Created by uncle-R on 15/12/21.
//  Copyright © 2015年 uncle-R. All rights reserved.


    
    import UIKit
    
    class RXPictureView: UICollectionView {
        
        //MARK: - 注册ID
        private let ReuseIdentifier = "StatusPicture"
        
        var status:RXStatus? {
            
            didSet{
                
                self.reloadData()
            }
        }
        
        //流水布局
        private var layout = UICollectionViewFlowLayout()
        
        init(){
            super.init(frame: CGRectZero,collectionViewLayout:layout)
            //注册cell
            self.registerClass(RXPictureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
            backgroundColor = UIColor.redColor()
            //设置数据源
            self.dataSource = self
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        //计算cell的尺寸返回.
        func calcViewSize()-> CGSize{
            
           let itemSize = CGSize(width: 90, height: 90)
            
             let margin: CGFloat = 10
             layout.itemSize = itemSize
            
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            
            //获取图片的数量,如果没有值,默认为0
            
            let count = status?.pic_urls?.count ?? 0
            
            if count == 0 {
                return CGSizeZero
            }
            
            // 1张图片,返回图片的大小
            if count == 1 {
                let size = CGSize(width: 150, height: 120)
                // 设置itemSize
                layout.itemSize = size
                
                // TODO: 返回一个固定的大小
                return size
            }
            
            // 设置间距
            layout.minimumInteritemSpacing = margin
            layout.minimumLineSpacing = margin

            // 4张图片
            if count == 4 {
                let value = 2 * itemSize.width + margin
                
                return CGSize(width: value, height: value)
            }
            
            // 其他情况
            // 列数,行数计算依赖列数,column必须为Int
            let column = 3
            
            // 宽度 = 列数 * cell的宽度 + (列数 - 1) * 间距
            let width = CGFloat(column) * itemSize.width + (CGFloat(column) - 1) * margin
            let row = (count + column - 1) / column
            
            // 高度 = 行数 * cell的高度 + (行数 - 1) * 间距
            let height = CGFloat(row) * itemSize.height + (CGFloat(row) - 1) * margin
            return CGSize(width: width, height: height)
   
            
        }

        
    }
    
    ///利用拓展实现数据源方法.
    extension RXPictureView: UICollectionViewDataSource{
    
        //返回图片 celL的数量
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            // 没有图片返回0
            return status?.pic_urls?.count ?? 0
        }
    
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            // 去缓存池中查找cell
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! RXPictureCell
    
            // 显示图片.设置图片的url
            let urlString = status?.pic_urls?[indexPath.item]["thumbnail_pic"] as! String
    
            // 创建NSURL
            let url = NSURL(string: urlString)
            cell.imageURL = url
    
            return cell
        }
    
    
    
    }

    class RXPictureCell:UICollectionViewCell {
        
        //当外界赋值一个URL 地址 自动加载图片
        var imageURL :NSURL? {
            didSet{
                //显示图片
                imageView.sd_setImageWithURL(imageURL)
            }
            
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            prepareUI()
            
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        //准备UI
        func prepareUI(){
            
            //
            contentView.addSubview(imageView)
            //添加约束
            imageView.ff_Fill(contentView)
            
        }
        //MARK: -懒加载图片
        //TODO:等会在看
        private lazy var imageView :UIImageView = UIImageView()
        
        
        
    }
    

